import Sockette from 'sockette';
import apiConfig from '../config/api.config';

const { wsProtocol, port, host, wsPath } = apiConfig;
const ws = function ({ onopen, onmessage, onreconnect, onmaximum, onclose, onerror }, opt = {}) {
  const url = `${wsProtocol}${host}${opt.port ? ':' + opt.port : port ? ':' + port : ''}${opt.url}`;
  return new Sockette(url, {
    timeout: 5e3,
    maxAttempts: 10,
    onopen: onopen || (e => console.log('Connected!', e)),
    onmessage: onmessage || (e => console.log('Received:', e)),
    onreconnect: onreconnect || (e => console.log('Reconnecting...', e)),
    onmaximum: onmaximum || (e => console.log('Stop Attempting!', e)),
    onclose: onclose || (e => console.log('Closed!', e)),
    onerror: onerror || (e => console.log('Error:', e))
  });
};

export default ws;
